package com.jxt.myjxtmanager.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jxt.myjxtmanager.BaseActivity;
import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.db.Student;
import com.jxt.myjxtmanager.db.StudentModel;
import com.jxt.myjxtmanager.utils.ToastUtil;

/**
 * Created by qm on 2017/3/7 0007.
 */
public class AddStudentActivity extends BaseActivity {

    private EditText et_student_name;
    private EditText et_student_num;
    private EditText et_sex;
    private EditText et_birthday;
    private EditText edt_class;
    private EditText ed_parent_name;
    private EditText ed_parent_phone;
    private EditText ed_urgent_phone;
    private EditText edt_address;

    //    private RelativeLayout rl_select_address;
    private Button bt_aphone_next;
    private Student student;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_student;
    }


    @Override
    protected void eventBundle(Bundle bundle) {
        super.eventBundle(bundle);
        if (bundle != null) {
            student = (Student) bundle.getSerializable("student");
        }
    }

    @Override
    protected void initView() {
        setToolbarTitle("添加学生信息");
        setToolbarNavigationIcon(R.drawable.ic_left_return);
        et_student_name = (EditText) findViewById(R.id.et_student_name);
        et_student_num = (EditText) findViewById(R.id.et_student_num);
        et_sex = (EditText) findViewById(R.id.et_sex);
        et_birthday = (EditText) findViewById(R.id.et_birthday);
        edt_class = (EditText) findViewById(R.id.edt_class);
        ed_parent_name = (EditText) findViewById(R.id.ed_parent_name);
        ed_parent_phone = (EditText) findViewById(R.id.ed_parent_phone);
        ed_urgent_phone = (EditText) findViewById(R.id.ed_urgent_phone);
        edt_address = (EditText) findViewById(R.id.edt_address);
//        rl_select_address = (RelativeLayout) findViewById(R.id.rl_select_address);
        bt_aphone_next = (Button) findViewById(R.id.bt_aphone_next);

//        rl_select_address.setOnClickListener(this);
        bt_aphone_next.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (student != null) {
            et_student_name.setText(student.getStudentName());
            et_student_num.setText(student.getStudentNum());
            et_sex.setText(student.getSex());
            et_birthday.setText(student.getBirthday());
            edt_class.setText(student.getMyClass());
            ed_parent_name.setText(student.getParentName());
            ed_parent_phone.setText(student.getParentPhone());
            ed_urgent_phone.setText(student.getUrgentPhone());
            edt_address.setText(student.getAddress());
            et_student_num.setFocusable(false);
            et_sex.setFocusable(false);
            et_birthday.setFocusable(false);
            edt_class.setFocusable(false);
            ed_parent_name.setFocusable(false);
        }
    }

    @Override
    public void onClickLeftMenu() {
        super.onClickLeftMenu();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_aphone_next:
                if (isverify()) {
                    Student student1;
                    if (student != null) {
                        student1 = new Student(student.getId());
                    } else {
                        student1 = new Student(System.currentTimeMillis());
                    }
                    student1.setStudentNum(et_student_num.getText().toString().trim());
                    student1.setStudentName(et_student_name.getText().toString().trim());
                    student1.setSex(et_sex.getText().toString().trim());
                    student1.setBirthday(et_birthday.getText().toString().trim());
                    student1.setAddress(edt_address.getText().toString().trim());
                    student1.setParentName(ed_parent_name.getText().toString().trim());
                    student1.setParentPhone(ed_parent_phone.getText().toString().trim());
                    student1.setUrgentPhone(ed_urgent_phone.getText().toString().trim());
                    student1.setMyClass(edt_class.getText().toString().trim());
                    if (student != null) {
                        StudentModel.getInstance().update(student1);
                        ToastUtil.showToast(AddStudentActivity.this, "修改成功");
                    } else {
                        StudentModel.getInstance().insertOrReplace(student1);
                        ToastUtil.showToast(AddStudentActivity.this, "添加成功");
                    }
                    finish();
                }
                break;
        }
    }

    private boolean isverify() {
        if (TextUtils.isEmpty(et_student_name.getText().toString().trim()) ||
                TextUtils.isEmpty(et_student_num.getText().toString().trim()) ||
                TextUtils.isEmpty(et_sex.getText().toString().trim()) ||
                TextUtils.isEmpty(et_birthday.getText().toString().trim()) ||
                TextUtils.isEmpty(edt_class.getText().toString().trim()) ||
                TextUtils.isEmpty(ed_parent_name.getText().toString().trim()) ||
                TextUtils.isEmpty(ed_parent_phone.getText().toString().trim()) ||
                TextUtils.isEmpty(edt_address.getText().toString().trim()) ||
                TextUtils.isEmpty(ed_urgent_phone.getText().toString().trim())) {
            ToastUtil.showToast(this, "请检查信息完整");
            return false;
        }
        return true;
    }
}
